export default class DragItem {
	constructor(con, item, stage) {
		this.offset = { x: 0, y: 0 };
		this.resizeOffset = { x: 0, y: 0 };
		this.container = con;
		this.container.item = item;
		this.stage = stage;

		this.container.item.mouseChildren = false;
		this.container.item.cursor = "pointer";
		
		this.container.gotoAndStop(0);
		
		
		this.container.btnResize.mouseChildren = false;
		this.container.btnRotate.mouseChildren = false;
		this.container.btnRemove.mouseChildren = false;
		
		let hit = new window.createjs.Shape();
		hit.graphics.beginFill("#000").drawRect(-item.nominalBounds.width/2, -item.nominalBounds.height/2, item.nominalBounds.width, item.nominalBounds.height);
		this.container.item.hitArea = hit;


		this.refresh();
		this.isVisibleBtns(false);
		this.container.addChild(item);

	}

	initItem() {
		this.container.btnRemove.on("click", () => {
			this.removeItem();
		});

		this.container.btnResize.on("mousedown", (e) => {
			e.stopPropagation();
			this.resizeOffset.x = e.stageX;
			this.resizeOffset.y = e.stageY;
		});
		
		this.container.btnResize.on("pressmove", (e) => {
			e.stopPropagation();
			let xx = e.stageX - this.resizeOffset.x;
			let yy = e.stageY - this.resizeOffset.y;
			let len = Math.sqrt(xx * xx + yy * yy);
			if(xx > 0){
				this.container.item.scaleX = this.container.item.scaleY = 1 + len/this.container.item.nominalBounds.width;
			}else{
				let scale = 1 - len/this.container.item.nominalBounds.width;
				if(scale < 0.5){
					scale = 0.5
				}
				this.container.item.scaleX = this.container.item.scaleY = scale;
			}
			this.refresh();
		});
		
		let angle2;
		let angle;
		let angle1;
		this.container.btnRotate.on("pressmove", (e) => {
			e.stopPropagation();
			let anchorPoint = {
				x: e.currentTarget.parent.x,
				y: e.currentTarget.parent.y
			};
			let keyPoint = this.stage.globalToLocal(e.stageX, e.stageY);
			angle2 = Math.atan((keyPoint.y - anchorPoint.y) / (keyPoint.x - anchorPoint.x));
			e.currentTarget.parent.rotation = (this.getQuadrant(keyPoint.x, anchorPoint.x) + angle2 - angle1 + angle) * 180 / Math.PI;
		});
		
		this.container.btnRotate.on("mousedown", (e) => {
			e.stopPropagation();
			let arrow_ = e.currentTarget;
			angle = arrow_.parent.rotation * Math.PI / 180;
			let anchorPoint = {
				x: arrow_.parent.x,
				y: arrow_.parent.y
			};
			let keyPoint = this.stage.globalToLocal(e.stageX, e.stageY);
			angle1 = Math.atan((keyPoint.y - anchorPoint.y) / (keyPoint.x - anchorPoint.x)) + this.getQuadrant(keyPoint.x, anchorPoint.x);
		});
		
		this.container.item.on("mousedown", (e) => {
			e.stopPropagation();
			let point = this.stage.globalToLocal(e.stageX, e.stageY);
			this.offset.x = this.container.x - point.x;
			this.offset.y = this.container.y - point.y;
		});
		
		this.container.on("click", () => {
			// e.stopPropagation();
			window.setTimeout(() => {
				this.isVisibleBtns(true);
			}, 50);
		});
		
		this.container.item.on("pressmove", (e) => {
			e.stopPropagation();
			let point = this.stage.globalToLocal(e.stageX, e.stageY);

			this.container.x = point.x + this.offset.x;
			this.container.y = point.y + this.offset.y;
		});
	}

	getQuadrant(keyX, anchorX) {
		return keyX - anchorX >= 0 ? 0 : Math.PI;
	}

	refresh() {
		let item = this.container.item;
		let wid = item.nominalBounds.width * item.scaleX;
		let hei = item.nominalBounds.height * item.scaleX;
		this.container.btnResize.x = wid/2 + this.container.btnResize.nominalBounds.height/2;
		this.container.btnResize.y = -hei/2 - this.container.btnResize.nominalBounds.height/2;
		
		this.container.btnRotate.x = wid;
		this.container.btnRotate.y = hei/2;
		
		this.container.btnRemove.x = -wid/2 - this.container.btnRemove.nominalBounds.width/2;
		this.container.btnRemove.y = hei/2 + this.container.btnRemove.nominalBounds.height/2;
	
	}

	isVisibleBtns(isVisible) {
		this.container.btnResize.visible = isVisible;
		this.container.btnRotate.visible = isVisible;
		this.container.btnRemove.visible = isVisible;
	}
	
	addItem(parent_) {
		this.initItem();
		parent_.addChild(this.container);
	}

	resetItem() {
		this.container.btnResize.removeAllEventListeners();
		
		this.container.btnRotate.removeAllEventListeners();
		
		this.container.btnRemove.removeAllEventListeners();
		
		this.container.removeAllEventListeners();
	}

	removeItem() {
		this.resetItem();
		if (this.container && this.container.parent) {
			this.container.parent.removeChild(this.container);
		}
		this.container.dispatchEvent("destroy");
	}

	getItem() {
		return this.container;
	}
}
